package com.lanren.familybookapp.ui.me.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.common.utils.GsonUtils;
import com.lanren.common.utils.LogUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.common.widget.ClearEditText;
import com.lanren.common.widget.TitleBarView;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.ui.me.adapter.InviteAdapter;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IBookEditModel;
import com.lanren.familybookapp.ui.me.model.BookEditModel;
import com.lanren.familybookapp.ui.me.presenter.BookEditPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @ClassName BookEditActivity
 * @Description TODO 账本创建添加和编辑
 * @Author chongheng.wang
 * @Date 2019/11/5 14:15
 * @Version 1.0
 */
public class BookEditActivity extends BaseUiActivity<BookEditPresenter> implements IBookEditModel.IBookEditView {

    @BindView(R.id.title_view)
    TitleBarView titleView;
    @BindView(R.id.book_name)
    ClearEditText bookName;
    @BindView(R.id.book_creater)
    ClearEditText bookCreater;
    @BindView(R.id.book_budget)
    ClearEditText bookBudget;
    @BindView(R.id.book_invite)
    ClearEditText bookInvite;
    @BindView(R.id.book_invite_add)
    TextView bookInviteAdd;
    @BindView(R.id.book_invite_list)
    RecyclerView bookInviteList;

    private List<Map<String, String>> list = new ArrayList<>();
    private InviteAdapter inviteAdapter;
    private int bookId;
    private boolean isEdit = true;

    /**
     * @param whence 1账本管理  2主页
     */
    public static void startActivity(Context context, int whence, BookInfoBean bookInfoBean) {
        Intent intent = new Intent(context, BookEditActivity.class).putExtra("whence", whence).putExtra("data", bookInfoBean);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, int whence, int bookId) {
        Intent intent = new Intent(context, BookEditActivity.class).putExtra("whence", whence).putExtra("bookId", bookId);
        context.startActivity(intent);
    }

    @Override
    public BookEditPresenter createPresenter() {
        return new BookEditPresenter(new BookEditModel());
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_book_edit;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
        initData();
        initEvent();
    }


    @Override
    public void initView() {
        bookInviteList.setLayoutManager(new LinearLayoutManager(this));

        inviteAdapter = new InviteAdapter(this, list);
        bookInviteList.setAdapter(inviteAdapter);

        setEditStatusOff();
    }

    @Override
    public void initData() {
        int whence = getIntent().getIntExtra("whence", 0);
        if (whence == 1) {
            BookInfoBean bookInfoBean = (BookInfoBean) getIntent().getSerializableExtra("data");
            bookId = bookInfoBean.getId();
            bookName.setText(bookInfoBean.getBookName());
            bookCreater.setText(bookInfoBean.getBookCreateUser());
            if (bookInfoBean.getBookBudget() == null) {
                bookBudget.setText("");
            } else {
                bookBudget.setText(bookInfoBean.getBookBudget().toString());
            }
            List<Map<String, String>> data = GsonUtils.fromJsonToListMaps(bookInfoBean.getUserInvite());
            list.addAll(data);
            inviteAdapter.notifyDataSetChanged();
        } else if (whence == 2) {
            bookId = getIntent().getIntExtra("bookId", 0);
            mPresenter.getBookList(bookId);
        }

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void initEvent() {
        titleView.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleView.setRightTxtListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    setEditStatusOn();
                } else {
                    setEditStatusOff();
                    LogUtils.d(list.size() + "  update  " + GsonUtils.toJson(list));
                    String invite = GsonUtils.toJson(list);
                    mPresenter.updateBook(bookId, bookName.getText().toString(), bookBudget.getText().toString(), invite);
                }
            }
        });


        inviteAdapter.setAdapterItemClickView(new AdapterItemClickView() {
            @Override
            public void onItemClick(int pointion) {
                inviteAdapter.remove(pointion);
                if (list.size() > 0) {
                    LogUtils.d(list.size() + "  remove  " + GsonUtils.toJson(list));
                }
            }

            @Override
            public void onItemClick(int pointion, View view) {

            }

            @Override
            public void onItemLongClick(int pointion) {

            }

            @Override
            public void onItemLongClick(int pointion, View view) {

            }
        });

        bookInviteAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEdit) {
                    String invite = bookInvite.getText().toString();
                    if (StringUtils.isNotBlank(invite)) {
                        Map<String, String> hashMap = new HashMap<>();
                        hashMap.put("userInvite", invite);
                        inviteAdapter.add(0, hashMap);
                        bookInvite.setText("");
                        if (list.size() > 0) {
                            LogUtils.d(list.size() + "  add  " + GsonUtils.toJson(list));
                        }
                    } else {
                        showMessage("记账人员输入不能为空！");
                    }
                }
            }
        });

    }


    private void setEditStatusOn() {
        isEdit = false;

        titleView.setRightTxt("保存");
        bookName.setClearIconVisible(true);
        bookCreater.setClearIconVisible(false);
        bookBudget.setClearIconVisible(true);

        bookName.setEnabled(true);
        bookBudget.setEnabled(true);
        bookInvite.setEnabled(true);

        bookInviteAdd.setSelected(true);
        bookInviteAdd.setEnabled(true);

        inviteAdapter.setDele(true);
    }

    private void setEditStatusOff() {
        isEdit = true;
        titleView.setRightTxt("编辑");
        bookName.setClearIconVisible(false);
        bookCreater.setClearIconVisible(false);
        bookBudget.setClearIconVisible(false);

        bookName.setEnabled(false);
        bookCreater.setEnabled(false);
        bookBudget.setEnabled(false);
        bookInvite.setEnabled(false);

        bookInviteAdd.setSelected(false);
        bookInviteAdd.setEnabled(false);

        inviteAdapter.setDele(false);

    }

    @Override
    public void onSuccess(String msg) {
        showMessage(msg);
        setEditStatusOff();
    }

    @Override
    public void GetBookEditSuccess(List<BookInfoBean> bookInfoBeans) {
        if (bookInfoBeans.size() > 0) {
            BookInfoBean bookInfoBean = bookInfoBeans.get(0);
            bookName.setText(bookInfoBean.getBookName());
            bookCreater.setText(bookInfoBean.getBookCreateUser());
            if (bookInfoBean.getBookBudget() == null) {
                bookBudget.setText("");
            } else {
                bookBudget.setText(bookInfoBean.getBookBudget().toString());
            }
            List<Map<String, String>> data = GsonUtils.fromJsonToListMaps(bookInfoBean.getUserInvite());
            list.addAll(data);
            inviteAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError(int code, String msg) {
        super.showError(code, msg);
    }
}
